<template>
  <el-dialog
    class="boundDialog"
    v-model="props.batchDialog"
    title="新增"
    width="25%"
    :show-close="false"
  >
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
    >
      <el-form-item
        label="入库批次"
        prop="batchNum"
        :rules="{
          required: true,
          message: '请输入入库批次',
          trigger: 'blur'
        }"
        style="width: 90%"
      >
        <el-input
          v-model="ruleForm.batchNum"
          autocomplete="off"
          placeholder="请输入入库批次"
          maxlength="32"
          show-word-limit
        />
      </el-form-item>
      <el-form-item
        label="生产日期"
        prop="produceDate"
        :rules="{
          required: true,
          message: '请选择生产日期',
          trigger: 'change'
        }"
        style="width: 90%"
      >
        <el-date-picker
          v-model="ruleForm.produceDate"
          type="date"
          placeholder="生产日期"
          size="default"
          style="width: 100%"
          clearable
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item
        label="备件单价"
        style="width: 90%"
        prop="price"
        :rules="{
          required: true,
          message: '请填写单价',
          trigger: 'blur'
        }"
      >
        <el-input
          v-model="ruleForm.price"
          autocomplete="off"
          placeholder=""
          onkeyup="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g,'$1')"
          style="width: 93%"
        />元
      </el-form-item>
      <el-form-item>
        <el-button @click="cancel(ruleFormRef)"> 取消 </el-button>
        <el-button
          type="primary"
          @click="submit(ruleFormRef)"
          :loading="loading"
          >确认</el-button
        >
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script setup lang="ts">
import { ElMessage, FormInstance } from 'element-plus'
import { ref, reactive, watch } from 'vue'
import { savePartBatchApi } from '/@/api/spare'
import { verifiyNumberInteger } from '/@/utils/toolsValidate'
const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()
const emit = defineEmits(['cancelBatch', 'submitBatch'])
const loading = ref(false)
const props = defineProps({
  batchDialog: Boolean,
  partId: String
})
const ruleForm = ref<any>({
  partId: '',
  batchNum: '',
  // stockAmount: "",
  price: '',
  produceDate: ''
})
const cancel = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  emit('cancelBatch')
}
const submit = (formEl: FormInstance | undefined) => {
  loading.value = true
  if (!formEl) return
  formEl.validate((valid, fields) => {
    if (valid) {
      ruleForm.value.partId = props?.partId as string
      savePartBatchApi(ruleForm.value).then((res) => {
        if (res.code == 0) {
          ElMessage({
            type: 'success',
            message: '新增成功'
          })
          emit('submitBatch', ruleForm.value.batchNum)
        }
      })
      loading.value = false
    } else {
      loading.value = false
    }
  })
}

const priceInput = (val: any) => {
  ruleForm.value.price = verifiyNumberInteger(val)
}

watch(
  () => props.batchDialog,
  () => {
    if (props.batchDialog == true) {
      ruleForm.value = {}
    }
  }
)
</script>
<style scoped lang="scss"></style>
